package com.bookstore.repository;

import com.bookstore.entity.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CategoryRepository extends JpaRepository<Category, Long> {
    
    // 查找所有顶级分类（没有父分类的）
    @Query("SELECT c FROM Category c WHERE c.parent IS NULL ORDER BY c.displayOrder ASC")
    List<Category> findAllParentCategories();
    
    // 根据父分类ID查找子分类
    List<Category> findByParentIdOrderByDisplayOrderAsc(Long parentId);
    
    // 根据分类名称查找
    Category findByCategoryName(String categoryName);
}